import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/parts/api/v1';

const getListDesc = formatMessage({
    id: 'api.getList',
    defaultMessage: '查询备件需求计划单'
});

const getDetailDesc = formatMessage({
    id: 'api.getDetail',
    defaultMessage: '查询备件需求计划单详情'
});

const newPartOrderPlanDesc = formatMessage({
    id: 'api.newPartOrderPlanDesc',
    defaultMessage: '新增备件需求计划单'
});

const modifyPartOrderPlanDesc = formatMessage({
    id: 'api.modifyPartOrderPlanDesc',
    defaultMessage: '修改备件需求计划单'
});

const abandonPartOrderPlanDesc = formatMessage({
    id: 'api.abandonPartOrderPlanDesc',
    defaultMessage: '作废备件需求计划单'
});

const submitPartOrderPlanDesc = formatMessage({
    id: 'api.submitPartOrderPlanDesc',
    defaultMessage: '提交备件需求计划单'
});

const approvePassPartOrderPlanDesc = formatMessage({
    id: 'api.approvePassPartOrderPlanDesc',
    defaultMessage: '审核通过备件需求计划单'
});

const approveRejectPartOrderPlanDesc = formatMessage({
    id: 'api.approveRejectPartOrderPlanDesc',
    defaultMessage: '驳回备件需求计划单'
});

const exportPartOrderPlanDesc = formatMessage({
    id: 'api.exportPartOrderPlanDesc',
    defaultMessage: '导出备件需求计划单'
});

const getPartsDesc = formatMessage({
    id: 'api.getPartsDesc',
    defaultMessage: '查询总代配件信息'
});

const importTempDesc = formatMessage({
    id: 'api.importTempDesc',
    defaultMessage: '下载导入清单模板'
});

const getHistoryListDesc = formatMessage({
    id: 'api.getHistoryListDesc',
    defaultMessage: '查询备件需求计划历史'
});

const getHistoryDetailDesc = formatMessage({
    id: 'api.getHistoryDetailDesc',
    defaultMessage: '查询备件需求计划历史详情'
});
// 初始化查询
export const initQuery = () => request(`${baseUrl}/ui/partOrderPlan/init`, null, dto.InitData)
    .then(handleFetchResultNotification(REQUEST_TYPE.initQuery));

// 查询经销商列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/partOrderPlans/forCurrentUser?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询大区数据详情
export const getDetail = id => request(`${baseUrl}/partOrderPlans/${id}`, null, dto.Detail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

//新增
export const newPartOrderPlan = data => request(`${baseUrl}/partOrderPlans`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newPartOrderPlanDesc));
//修改
export const modifyPartOrderPlan = (id, data) => request(`${baseUrl}/partOrderPlans/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyPartOrderPlanDesc));

//作废
export const abandonPartOrderPlan = (id, data) => request(`${baseUrl}/partOrderPlans/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonPartOrderPlanDesc));

//提交
export const submitPartOrderPlan = (id, data) => request(`${baseUrl}/partOrderPlans/${id}/submit`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, submitPartOrderPlanDesc));

//审核通过
export const approvePartOrderPlanPass = (id, data) => request(`${baseUrl}/partOrderPlans/${id}/pass`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approvePassPartOrderPlanDesc));

//驳回
export const approvePartOrderPlanReject = (id, data) => request(`${baseUrl}/partOrderPlans/${id}/reject`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, approveRejectPartOrderPlanDesc));

// 导出
export const exportPartOrderPlans = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partOrderPlans/export/forCurrentUser?${queryString}`, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportPartOrderPlanDesc))
        .then(res => res.data);
};

// 查询总代配件信息
export const getParts = pageOption => {
    const queryString = stringify(pageOption);
    return request(`${baseUrl}/parts/forPartOrderPlan?${queryString}`, null, dto.PartList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getPartsDesc));
};

export const partOrderPlanPartImportTemp = () => request(`${baseUrl}/partOrderPlans/query/importTemp`)
    .then(handleFetchResultNotification(REQUEST_TYPE.submit, importTempDesc)).then(res => res.data);
//备件需求计划历史查询
export const getHistoryList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/partOrderPlans/forHistory/forCurrentUser?${queryString}`, null, dto.HistoryList)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getHistoryListDesc));
};

//查询备件需求计划历史详情
export const getHistoryDetail = id => request(`${baseUrl}/partOrderPlans/${id}/forHistory`, null, dto.HistoryDetail)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getHistoryDetailDesc));
